package cn.com.scitc.publicModule;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.HashMap;

public class Sample {

    //设置APPID/AK/SK
    public static final String APP_ID = "17756092";
    public static final String API_KEY = "PHM3qU7yMtxRixCwbn5FzVdz";
    public static final String SECRET_KEY = "zS9Wyl8zYMXv8zyvvoquMBlFuICIePNn";

    public String[] Sample(){
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        JFileChooser chooser = new JFileChooser();
        FileSystemView fsv = FileSystemView.getFileSystemView();
//        chooser.setCurrentDirectory(fsv.getHomeDirectory());
        chooser.setCurrentDirectory(new File("/Volumes/Document/GoogleDownloadFiles/车辆车牌号"));
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setDialogTitle("图片文件上传");
        chooser.setApproveButtonText("确定");
        int value = chooser.showOpenDialog(null);
        String path =  "";
        if(value == JFileChooser.APPROVE_OPTION){
            path = chooser.getSelectedFile().getPath();
        }
        if(path != ""){
            JSONObject res = client.plateLicense(path, new HashMap<String, String>());
            return new String[]{path, res.toString(2)};
        }
        return new String[]{};
    }
}
